package com.l.web.master.system.module;

import com.l.web.master.system.extension.Extension;
import com.l.web.master.system.extension.ExtensionDAO;
import org.l.qiao.Cluster;
import org.l.qiao.Page;
import org.l.qiao.builder.Field;
import org.l.qiao.builder.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ModuleService {

    @Autowired
    private ModuleDAO moduleDAO;
    @Autowired
    private ExtensionDAO extensionDAO;

    public ModuleService() {
    }

    public  List<Cluster> getPageList(Page page) {
        Field field = new Field();
        field.include(Module.TITLE, Module.EXT_KEY, Module.POSITION, Module.UPDATED);
        List<Cluster> list = moduleDAO.findPage(null, field, null, page);
        return list;
    }

    public List<Cluster> getExts( List<Cluster> list) {
        Field field = new Field();
        field.include(Extension.NAME);
        List<Object> keys = new ArrayList<>();

        for (Map<String, Object> mod : list) {
            Object obj = mod.get(Module.EXT_KEY);

            if (!keys.contains(obj)) {
                keys.add(obj);
            }
        }

        Query query = new Query();
        query.in(Extension.KEY, keys);
        List<Cluster> exts = extensionDAO.find(query, field, null);
        return exts;
    }
}
